﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GamePlugin.Logics.Core;
using WindowsInput;
using WindowsInput.Native;
using GamePlugin.Logics.Data;
using System.Windows.Forms;

namespace GamePlugin.Logics.Game.Actions
{
    public class DActionRunItemSearchStart : MachineState
    {
        public static bool FromIdleState = false;

        public override void Initialize()
        {
            this.AddState("DetermineNextStep");
            this["DetermineNextStep"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.DetermineNextStep(o, e));

            this.DefaultState = "DetermineNextStep";
        }

        private void CompareSearchItem()
        {
            Workspace.Instance.SearchItemCompare.GetChangedInfo(Workspace.Instance.SearchItem, Workspace.Instance.LastSearchItem);
        }

        private void TryChooseNext()
        {
            if (FromIdleState)
            {
                FromIdleState = false;
                Workspace.Instance.LastSearchItem = null; ;
            }
            else
            {
                var box = Helper.GetMainWindow().searchInfoList.searchListBox;
                int current = box.SelectedIndex;
                current++;
                if (current >= box.Items.Count)
                    current = 0;
                box.SelectedIndex = current;
            }

            if (Workspace.Instance.SearchItem.IsAvailable)
            {
                this.IsNeedReset = true;
                this.Owner.ChangeToState(typeof(DActionRunInputItemName).Name);
                this.CompareSearchItem();
            }
        }

        public void DetermineNextStep(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;
            Helper.MoveCursor(ActionData.ResetPoint);
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            switch (Workspace.Instance.SearchOptionData.RunMode)
            {
                case (int)ERunMode.None:
                    break;

                case (int)ERunMode.TestSingleMode:
                    if (Workspace.Instance.SearchItem.IsAvailable)
                    {
                        this.IsNeedReset = true;
                        this.Owner.ChangeToState(typeof(DActionRunInputItemName).Name);
                        CompareSearchItem();
                    }
                    else
                    {
                        MessageBox.Show("請激活當前選項");
                    }
                    break;

                case (int)ERunMode.TestAllMode:
                    this.TryChooseNext();
                    break;

                case (int)ERunMode.NormalMode:
                    this.TryChooseNext();
                    break;

                case (int)ERunMode.TestAllWithLowValuesMode:
                    this.TryChooseNext();
                    break;
                default:
                    throw new Exception("Not valid");
            }
        }
    }
}
